package com.lanou.dllo.soccernews.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.CircleShowBean;
import com.lanou.dllo.soccernews.view.CustomGridView;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/10/30.
 */
public class ShowCircleAdapter extends BaseExpandableListAdapter {
    private List<CircleShowBean> circleShowBeans;
    // private List<CircleShowBean.GroupsEntity> groupsEntities;

    private LayoutInflater mInflater;
    private Context context;

    public ShowCircleAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

        this.context = context;
    }



    public void addDatas(List<CircleShowBean> circleShowBeans) {
        this.circleShowBeans = circleShowBeans;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return circleShowBeans != null && circleShowBeans.size() > 0 ? circleShowBeans.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public CircleShowBean getGroup(int groupPosition) {
        return circleShowBeans.get(groupPosition);
    }

    @Override
    public List<CircleShowBean.GroupsEntity> getChild(int groupPosition, int childPosition) {
        return circleShowBeans.get(groupPosition).getGroups();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandableListViewHodle hodle;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_expandsblelist, null);
            hodle = new ExpandableListViewHodle();
            hodle.groupName = (TextView) convertView.findViewById(R.id.group_name);
            convertView.setTag(hodle);

        } else {
            hodle = (ExpandableListViewHodle) convertView.getTag();
        }

        CircleShowBean circle = getGroup(groupPosition);
        if (circle != null) {
            hodle.groupName.setText(circle.getName());
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


        GetGridView gridviewHodle;
        if (convertView == null) {
            gridviewHodle = new GetGridView();

            convertView = mInflater.inflate(R.layout.gridview_expandable, parent,false);
            gridviewHodle.gridview = (CustomGridView) convertView.findViewById(R.id.channel_item_child_gridView);

            convertView.setTag(gridviewHodle);

        } else {

            gridviewHodle = (GetGridView) convertView.getTag();
        }

        List<CircleShowBean.GroupsEntity> groups = getChild(groupPosition,childPosition);
       // Log.d("ShowCircleAdapter", "groups:" + groups);

        CircleGridViewAdapter adapter = new CircleGridViewAdapter(context, groups);
        gridviewHodle.gridview.setAdapter(adapter);


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    class ExpandableListViewHodle {
        private TextView groupName;
    }

    class GetGridView {
        private CustomGridView gridview;
    }
}
